package reader

import (
	"fmt"
	"reflect"
	"strings"
)

func (r *Reader) WriteInStruct(key string, in *interface{}) interface{} {
	r.UnmarshalKey(key, in)
	return in
}
func (r *Reader) WriteString(key string) string {
	return r.GetString(key)
}

func (r *Reader) WriteInt(key string) int {
	return r.GetInt(key)
}

func (r *Reader) WriteMap(key string) map[string]any {
	return r.GetStringMap(key)
}

func (r *Reader) WriteInt32(key string) int32 {
	return r.GetInt32(key)
}


func InParam(k1 reflect.Value, value any) error {
	var err error
	if !reflect.ValueOf(value).IsValid() {
		return fmt.Errorf("1001,%s", "值不能为空")
	}
	if k1.Kind() == reflect.Struct {
		kt := k1.Type()
		kk1 := reflect.New(kt).Elem()
		for i := 0; i < kk1.NumField(); i++ {
			if _, ok := value.(map[string]any); !ok {
				return fmt.Errorf("1002,%s", "值类型不对")
			}
			err = InParam(kk1.Field(i), value.(map[string]any)[strings.ToLower(kt.Field(i).Name)])
			if err != nil {
				return err
			}
		}
		k1.Set(kk1)
		return err
	}
	k1.Set(reflect.ValueOf(value))
	return err
}
